package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传图片,并保存到素材表
     *
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult upload_picture(MultipartFile multipartFile) {
        //1.检查参数
        if (multipartFile == null && multipartFile.getSize() == 0) {
            //返回无效参数
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.上传参数
        //UUID设置图片名,去掉"-"
        String imgName = UUID.randomUUID().toString().replace("-", "");
        //获得原始文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //获取图片后缀名
        String postfix = originalFilename.substring(originalFilename.lastIndexOf("."));
        imgName = imgName + postfix;
        String fileUrl = null;
        try {
            //上传图片到minIO,并获得地址
            fileUrl = fileStorageService.uploadImgFile("", imgName, multipartFile.getInputStream());
            log.info("上传图片到MinIO中，fileId:{}", fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("WmMaterialServiceImpl-上传文件失败");
        }
        //3.将图片资源存到数据库
        WmMaterial material = new WmMaterial();
        material.setUserId(WmThreadLocalUtil.getUser().getId());
        material.setUrl(fileUrl);
        material.setIsCollection((short) 0);//是否收藏
        material.setType((short) 0);// 0图片 1视频
        material.setCreatedTime(new Date());
        this.save(material);


        return ResponseResult.okResult(material);
    }


    /**
     * 素材分页查询
     *
     * @param wmMaterialDto
     * @return
     */
    @Override
    public ResponseResult findList(WmMaterialDto wmMaterialDto) {
        //1.效验分页参数
        wmMaterialDto.checkParam();
        //2.构造分页查询
        Page<WmMaterial> wmMaterialPage = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        //3.构建条件查询
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //查询当前用户
        queryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());
        //按时间倒序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        //是否查询收藏
        if (wmMaterialDto.getIsCollection() == null && wmMaterialDto.getIsCollection() == 1) {
            queryWrapper.eq(WmMaterial::getIsCollection, wmMaterialDto.getIsCollection());
        }
        //4.查询
        wmMaterialPage = this.page(wmMaterialPage, queryWrapper);
        //封装返回值
        ResponseResult wmMaterialResponseResult = new PageResponseResult(
                wmMaterialDto.getPage(), wmMaterialDto.getSize(), (int) wmMaterialPage.getTotal()
        );
        wmMaterialResponseResult.setData(wmMaterialPage.getRecords());


        return wmMaterialResponseResult;
    }
}
